package com.zhibang.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhibang.dto.GroupTravelDTO;
import com.zhibang.entity.GroupTravelDetail;
import com.zhibang.entity.GroupTravelOrder;
import com.zhibang.mapper.GroupTravelDetailMapper;
import com.zhibang.service.GroupTravelOrderService;
import com.zhibang.mapper.GroupTravelOrderMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
* @author ZhuanZ（无密码）
* @description 针对表【group_travel_order(团游订单表)】的数据库操作Service实现
* @createDate 2025-06-26 14:20:37
*/
@Service
public class GroupTravelOrderServiceImpl extends ServiceImpl<GroupTravelOrderMapper, GroupTravelOrder>
    implements GroupTravelOrderService{
    @Resource
    GroupTravelDetailMapper groupTravelDetailMapper;

    @Override
    public GroupTravelOrder createOrder(GroupTravelDTO dto, int userId) {
        //获取团游详情
        GroupTravelDetail detail = groupTravelDetailMapper.selectById(dto.getId());
        //创建订单
        GroupTravelOrder order = new GroupTravelOrder();
        order.setUserId(userId);
        order.setAttractionId(dto.getId());
        order.setVisitDate(dto.getDate());
        order.setNumPeople(dto.getNum());
        //生成订单号  雪花id
        Snowflake snowflake = new Snowflake(1, 1);
        long id = snowflake.nextId();
        order.setOrderId(id);
        //计算总金额
        BigDecimal price = new BigDecimal(String.valueOf(detail.getPrice()));
        BigDecimal num= new BigDecimal(String.valueOf(dto.getNum()));
        BigDecimal totalMoney = price.multiply(num).setScale(2, BigDecimal.ROUND_HALF_UP);
        order.setTotalAmount(totalMoney);
        save(order);
        return order;
    }
}




